import allure
from appium.webdriver.common.appiumby import AppiumBy

from .add_member_ways_page import AddMemberWaysPage
from .manage_contact_page import ManageContactPage
from .member_info_page import MemberInfoPage
from .search_page import SearchPage
from ..base.base import Base
from ..utils.log_util import logger


class ContactPage(Base):
    __SEARCH_BUTTON = (AppiumBy.XPATH, '//*[@text="个人测试"]/../../../following-sibling::*/*[1]')
    __MANAGE_CONTACT = (AppiumBy.ID,"com.tencent.wework:id/lfl")
    __CREATOR_TAG = (AppiumBy.ID, 'com.tencent.wework:id/j_p')
    __MEMBERS = (AppiumBy.ID, 'com.tencent.wework:id/g1b')
    __BOTTOM_TEXT = (AppiumBy.XPATH,"//*[@class='androidx.recyclerview.widget.RecyclerView']/child::*")
    __ADD_MEMBER_BUTTON = "添加成员"

    __TOAST = (AppiumBy.XPATH, "//*[@class='android.widget.Toast']")
    pass

    @allure.step("滑动点击添加成员按钮，进入添加成员选择方式页")
    def goto_add_member_ways_page(self):
        self.swipe_find(self.__ADD_MEMBER_BUTTON).click()
        image_path = self.screenshot()
        allure.attach.file(image_path, name="点击添加成员按钮", attachment_type=allure.attachment_type.PNG)
        return AddMemberWaysPage(self.driver)

    @allure.step("点击搜索按钮，进入搜索页面")
    def goto_search_page(self):
        self.find_ele(self.__SEARCH_BUTTON).click()
        image_path = self.screenshot()
        allure.attach.file(image_path, name="点击搜索按钮", attachment_type=allure.attachment_type.PNG)
        return SearchPage(self.driver)

    @allure.step("获取toast提示")
    def get_toast(self):
        toast_text = self.find_ele(self.__TOAST).text
        image_path = self.screenshot()
        allure.attach.file(image_path, name="获取toast提示", attachment_type=allure.attachment_type.PNG)
        return toast_text

    @allure.step("跳转至个人信息页")
    def goto_member_info(self):
        member_eles = self.find_eles(self.__MEMBERS)
        del member_eles[0]
        if len(member_eles) > 1:
            member_eles[1].click()
        return MemberInfoPage(self.driver)

    @allure.step("跳转至指定用户信息页")
    def goto_clarity_member_info_page(self, name):
        self.swipe_find(name).click()
        image_path = self.screenshot()
        allure.attach.file(image_path, name="跳转指定用户信息页", attachment_type=allure.attachment_type.PNG)
        return MemberInfoPage(self.driver)

    @allure.step("跳转至管理通讯录页面")
    def goto_manage_contact_page(self):
        self.find_ele_wait_until_located(self.__MANAGE_CONTACT).click()
        image_path = self.screenshot()
        allure.attach.file(image_path, name="跳转至管理通讯录页面", attachment_type=allure.attachment_type.PNG)
        return ManageContactPage(self.driver)


